package com.worldstear.dao;

import com.worldstear.model.mysql.MysqlVariable;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Mapper
@Repository
public interface BaseDBMapper {
    @Select("show databases")
    List<String> showDatabases();

    @Select("show variables like '%version%'")
    List<MysqlVariable> showVersionVariables();

    @Select("<script> show procedure status <when test='db != null'> where Db like #{db}</when> </script>")
    List<Map<String, String>> showProcedures(String db);

    @Select("<script> show function status <when test='db != null'> where Db like #{db}</when> </script>")
    List<Map<String, String>> showFunctions(String db);

    @Select("<script> SELECT * from information_schema.VIEWS <when test='db != null'> WHERE TABLE_SCHEMA = #{db}</when> </script>")
    List<Map<String, String>> showViews(String db);

    @Select("<script> SELECT * from information_schema.TABLES <when test='db != null'> WHERE TABLE_SCHEMA = #{db}</when> </script>")
    List<Map<String, String>> showTables(String db);

    @Select("<script> SELECT * FROM information_schema.COLUMNS WHERE 1 = 1 <when test='tableSchema != null'> AND TABLE_SCHEMA = #{tableSchema}</when> AND TABLE_NAME = #{tableName} </script>")
    List<Map<String, String>> getTableColumns(String tableSchema, String tableName);

    @Select("SELECT * FROM `${tableSchema}`.`${tableName}`")
    List<Map<String, String>> getTableData(String tableSchema, String tableName);
}
